<?php defined('BASEPATH') || exit('No direct script access allowed');

/**
 * Admin Controller
 *
 * This class provides a base class for all admin-facing controllers.
 * It automatically loads the form, form_validation and pagination
 * helpers/libraries, sets defaults for pagination and sets our
 * Admin Theme.
 *
 * @package    Bonfire
 * @subpackage MY_Controller
 * @category   Controllers
 * @author     Bonfire Dev Team
 * @link       http://guides.cibonfire.com/helpers/file_helpers.html
 *
 */
class Admin_Controller extends Authenticated_Controller
{
    protected $pager;
    protected $limit;

    //--------------------------------------------------------------------

    /**
     * Class constructor - setup paging and keyboard shortcuts as well as
     * load various libraries
     *
     */
    public function __construct()
    {
        $this->autoload['libraries'][] = 'ui/contexts';

        parent::__construct();

        // Pagination config
        $this->pager = [
            'full_tag_open'    => '<div class="dataTables_paginate"><ul class="pagination">',
            'full_tag_close'   => '</ul></div>',
            'next_link'        => '&rarr;',
            'prev_link'        => '&larr;',
            'next_tag_open'    => '<li>',
            'next_tag_close'   => '</li>',
            'prev_tag_open'    => '<li>',
            'prev_tag_close'   => '</li>',
            'first_tag_open'   => '<li>',
            'first_tag_close'  => '</li>',
            'last_tag_open'    => '<li>',
            'last_tag_close'   => '</li>',
            'cur_tag_open'     => '<li class="active"><a href="#">',
            'cur_tag_close'    => '</a></li>',
            'num_tag_open'     => '<li>',
            'num_tag_close'    => '</li>',
            'use_page_numbers' => false,
        ];
        $this->limit = $this->settings_lib->item('site.list_limit');

        // load the keyboard shortcut keys
        $shortcut_data = [
            'shortcuts'     => config_item('ui.current_shortcuts'),
            'shortcut_keys' => $this->settings_lib->find_all_by('module', 'core.ui'),
        ];
        Template::set('shortcut_data', $shortcut_data);

        // Profiler Bar?
        if (ENVIRONMENT == 'development') {
            if ($this->settings_lib->item('site.show_profiler')
                && $this->auth->has_permission('Bonfire.Profiler.View')
            ) {
                // Profiler bar?
                $this->showProfiler(false);
            }
        }

        Assets::add_js(['plugins/jQuery/jQuery-2.2.0.min.js']);
        Assets::add_js(['js/jquery.core.image.upload.min']);

        // Basic setup
        Template::set_theme($this->config->item('template.admin_theme'), $this->config->item('template.default_theme'));
    }

    /**
     * 分页
     * @author   [author]
     * @DateTime 2017-01-04T15:28:49+0800
     * @return   [type]     [description]
     */
    public function createPages($pagerBaseUrl, $count, $limit, $pagerUriSegment = 5)
    {

        $this->load->library('pagination');
        $this->pager['base_url']    = $pagerBaseUrl;
        $this->pager['total_rows']  = $count;
        $this->pager['per_page']    = $limit;
        $this->pager['uri_segment'] = $pagerUriSegment;

        $this->pagination->initialize($this->pager);
    }

    /**
     * 成功返回
     * @param array  $data
     * @param string $message
     * @param int    $code
     */
    public function success($data = [], $message = '', $code = 200)
    {
        $return = [
            'code'    => $code ? $code : 200,
            'message' => $message ? $message : '',
            'data'    => $data ? $data : [],
        ];
        echo json_encode($return);
        exit();
    }

    /**
     * 失败返回
     * @param array  $data
     * @param string $message
     * @param int    $code
     */
    public function failure($data = [], $message = '', $code = 0)
    {
        $return = [
            'code'    => $code ? $code : 0,
            'message' => $message ? $message : '',
            'data'    => $data ? $data : [],
        ];
        echo json_encode($return);
        exit();
    }
}
/* End of file Admin_Controller.php */
